package com.esfak47.mediafetecher.plugin.download;

import com.esfak47.common.job.Job;
import com.esfak47.mediafetcher.core.event.EventSender;

import java.util.Date;

/**
 * @author tony
 */
public class DownloadJob implements Job {

    private final String id;
    private final FilePoint filePoint;
    private final String name;
    private boolean finish;
    private boolean cancelled;
    private boolean exitUnexpectedly;
    private int process = 0;
    private Date createTime;
    private Date finishTime;
    private EventSender eventSender;

    public DownloadJob(String id, String name, String fileName, String fileTargetPath, String url, EventSender
        eventSender) {
        this.id = id;
        this.filePoint = new FilePoint(url, fileTargetPath, fileName);
        this.name = name;
        this.createTime = new Date();
        this.eventSender = eventSender;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public boolean isFinish() {
        return finish;
    }

    @Override
    public void setFinish(boolean finish) {
        this.finish = finish;
    }

    @Override
    public boolean isCancelled() {
        return cancelled;
    }

    @Override
    public void setCancelled(boolean cancelled) {
        this.cancelled = cancelled;
    }

    @Override
    public boolean isExitUnexpectedly() {
        return exitUnexpectedly;
    }

    @Override
    public void setExitUnexpectedly(boolean exitUnexpectedly) {
        this.exitUnexpectedly = exitUnexpectedly;
    }

    @Override
    public int getProcess() {
        return process;
    }

    @Override
    public void setProcess(int process) {
        this.process = process;
    }

    @Override
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public Date getFinishTime() {
        return finishTime;
    }

    @Override
    public void setFinishTime() {
        this.finishTime = new Date();
    }

    @Override
    public boolean isAsync() {
        return true;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void run() {
        DownloadTask downloadTask = new DownloadTask(filePoint, new DownLoadJobListener(this), eventSender,this.id);
        downloadTask.start();

    }

    public static class DownLoadJobListener implements DownloadListener {
        private DownloadJob job;

        public DownLoadJobListener(DownloadJob job) {this.job = job;}

        @Override
        public void onFinished() {
            job.setFinish(true);
            job.setFinishTime();
        }

        @Override
        public void onProgress(float progress) {
            job.setProcess(((int)Math.floor(progress * 100)));
        }

        @Override
        public void onPause() {

        }

        @Override
        public void onCancel() {
            job.setCancelled(true);
        }

        @Override
        public void onError() {
            job.setExitUnexpectedly(true);

        }

    }
}

